<template>
  <div class="app-container calendar-list-container">
    <!--设置套餐信息  -->
    <div>
      <h2>设置默认设备套餐参数</h2>
    </div>
    <el-form
      :model="mealDetail"
      :rules="mealRules"
      ref="mealForm"
      label-position="right"
      label-width="200px"
      style="width: 80%"
    >
      <el-form-item label="充电收费类型" prop="chargeType">
        <el-select
          v-model="mealDetail.chargeType"
          placeholder="请选择收费类型"
          clearable
          class="filter-item"
          style="width: 200px"
        >
          <el-option label="按次" :value="1"></el-option>
          <el-option label="按时间段" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="充电收费价格(元/次)"
        prop="chargeAmount"
        v-if="this.mealDetail.chargeType == 1"
      >
        <el-input
          clearable
          v-model="mealDetail.chargeAmount"
          maxlength="12"
          type="number"
          placeholder=" 请输入充电收费价格(元/次)"
        ></el-input>
      </el-form-item>

      <el-form-item label="收费标准" v-if="this.mealDetail.chargeType == 2">
        <el-row>
          <el-col
            :span="6"
            v-for="(item, index) in mealDetail.chargePowerData"
            :key="index"
          >
            <div class="powerSize">
              <div class="powerSizeTop">
                <span>功率范围:</span>
                {{ item.spower }}-{{ item.epower }}W
              </div>
              <div>
                <div>
                  <span>峰值收费:</span>
                  {{ divide(item.chargePeakAmount,100) }}元
                </div>
                <div>
                  <span>低谷收费:</span>
                  {{ divide(item.chargeLowEbAmount,100) }}元
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-button
          type="primary"
          :loading="loading.create"
          @click="dialog.powerSetVisible = true"
          >重新设定</el-button
        >
      </el-form-item>

      <el-form-item
        label="电费收费时间间隔(分钟)"
        prop="amountTime"
        v-if="
          mealDetail.chargeType == 2 ||
          mealDetail.serviceType == 2 ||
          mealDetail.custodyType == 2
        "
      >
        <el-input
          clearable
          v-model="mealDetail.amountTime"
          maxlength="5"
          type="number"
          style="width: 200px"
          placeholder="请输入收费时间间隔(分钟)"
        ></el-input>
      </el-form-item>

      <el-form-item label="服务收费" prop="serviceType">
        <el-select
          v-model="mealDetail.serviceType"
          placeholder="请选择服务收费类型"
          clearable
          class="filter-item"
          style="width: 200px"
        >
          <el-option label="免费" :value="0"></el-option>
          <el-option label="按次收费" :value="1"></el-option>
          <el-option label="按时间段收费" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="服务收费(元/次)"
        prop="serviceAmount"
        v-if="this.mealDetail.serviceType == 1"
      >
        <el-input
          clearable
          v-model="mealDetail.serviceAmount"
          maxlength="12"
          type="number"
          placeholder="请输入每次服务收费金额(元/次)"
        ></el-input>
      </el-form-item>

      <!-- <el-form-item
        label="峰值时间段服务收费(元/每个收费时间间隔)"
        prop="servicePeakAmount"
        v-if="this.mealDetail.serviceType === 2"
      >
        <el-input
          clearable
          v-model="mealDetail.servicePeakAmount"
          maxlength="12"
          type="number"
          placeholder="请输入峰值时间段服务收费金额(元/每个收费时间间隔)"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="低谷时间段服务收费(元/每个收费时间间隔)"
        prop="serviceLowEbAmount"
        v-if="this.mealDetail.serviceType === 2"
      >
        <el-input
          clearable
          v-model="mealDetail.serviceLowEbAmount"
          maxlength="12"
          type="number"
          placeholder="请输入低谷时间段服务收费金额(元/每个收费时间间隔)"
        ></el-input>
      </el-form-item> -->
      

      <el-form-item label="收费标准" v-if="this.mealDetail.serviceType === 2">
        <el-row>
          <el-col
            :span="6"
            v-for="(item, index) in mealDetail.serviceData"
            :key="index"
          >
            <div class="powerSize">
              <div class="powerSizeTop">
                <span>功率范围:</span>
                {{ item.spower }}-{{ item.epower }}W
              </div>
              <div>
                <div>
                  <span>收费:</span>
                  {{ item.chargePeakAmount / 100 }}元
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-button
          type="primary"
          :loading="loading.create"
          @click="dialog.serveShow = true"
          >重新设定</el-button
        >
      </el-form-item>

      <el-form-item
        label="服务费时间间隔(分钟)"
        prop="serviceTime"
        v-if="this.mealDetail.serviceType === 2"
      >
        <el-input
          clearable
          v-model="mealDetail.serviceTime"
          maxlength="5"
          type="number"
          style="width: 200px"
          placeholder="请输入收费时间间隔(分钟)"
        ></el-input>
      </el-form-item>

      <el-form-item label="保管收费类型" prop="custodyType">
        <el-select
          v-model="mealDetail.custodyType"
          placeholder="请选择保管收费类型"
          clearable
          class="filter-item"
          style="width: 200px"
        >
          <el-option label="免费" :value="0"></el-option>
          <el-option label="按次收费" :value="1"></el-option>
          <el-option label="按时间段收费" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="保管收费(元/次)"
        prop="custodyAmount"
        v-if="this.mealDetail.custodyType == 1"
      >
        <el-input
          clearable
          v-model="mealDetail.custodyAmount"
          maxlength="12"
          type="number"
          placeholder="请输入每次保管收费金额(元/次)"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="峰值时间段保管收费(元/每个收费时间间隔)"
        prop="custodyPeakAmount"
        v-if="this.mealDetail.custodyType === 2"
      >
        <el-input
          clearable
          v-model="mealDetail.custodyPeakAmount"
          maxlength="12"
          type="number"
          placeholder="请输入峰值时间段保管收费金额(元/每个收费时间间隔)"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="低谷时间段保管收费(元/每个收费时间间隔)"
        prop="custodyLowEbAmount"
        v-if="this.mealDetail.custodyType === 2"
      >
        <el-input
          clearable
          v-model="mealDetail.custodyLowEbAmount"
          maxlength="12"
          type="number"
          placeholder="请输入低谷时间段保管收费金额(元/每个收费时间间隔)"
        ></el-input>
      </el-form-item>

      
      <el-form-item
        style="color: red"
        v-if="
          this.mealDetail.chargeType == 2 ||
          this.mealDetail.serviceType == 2 ||
          this.mealDetail.custodyType == 2
        "
      >
        <span>注意：峰值时间为 00:00 - 00:00 ，全天24小时皆为峰值</span>
      </el-form-item>
      <el-form-item
        label="峰值起始时间"
        prop="peakStartTime"
        v-if="
          this.mealDetail.chargeType == 2 ||
          this.mealDetail.serviceType == 2 ||
          this.mealDetail.custodyType == 2
        "
      >
        <el-col :span="11">
          <el-time-select
            v-model="mealDetail.peakStartTime"
            :picker-options="{
              start: '00:00',
              step: `00:30`,
              end: '23:30',
            }"
            placeholder="选择时间"
          ></el-time-select>
        </el-col>
      </el-form-item>

      <el-form-item
        label="峰值结束时间"
        prop="peakEndTime"
        v-if="
          this.mealDetail.chargeType == 2 ||
          this.mealDetail.serviceType == 2 ||
          this.mealDetail.custodyType == 2
        "
      >
        <el-col :span="11">
          <el-time-select
            v-model="mealDetail.peakEndTime"
            :picker-options="{
              start: '00:00',
              step: `00:${this.mealDetail.amountTime}`,
              end: '23:30',
            }"
            placeholder="选择时间"
          ></el-time-select>
        </el-col>
      </el-form-item>
      <el-form-item
        label="对应低谷时间为"
        v-if="
          this.mealDetail.chargeType == 2 ||
          this.mealDetail.serviceType == 2 ||
          this.mealDetail.custodyType == 2
        "
      >
        <el-col :span="11">
          {{ this.mealDetail.peakEndTime }}至
          {{ this.mealDetail.peakStartTime }}
        </el-col>
      </el-form-item>
      <!-- 123 -->
      <el-form-item label="单次最低收费(元)" prop="lowAmount">
        <el-input
          clearable
          v-model="mealDetail.lowAmount"
          maxlength="12"
          type="number"
          style="width: 200px"
          placeholder="请输入单次最低收费(元)"
        ></el-input>
      </el-form-item>
      <el-form-item label="单次最高收费(元)" prop="tallAmount">
        <el-input
          clearable
          v-model="mealDetail.tallAmount"
          maxlength="12"
          type="number"
          placeholder="请输入单次最高收费(元)"
          style="width: 200px"
        ></el-input>
      </el-form-item>
    </el-form>

    <div style="margin-left: 500px; margin-bottom: 50px">
      <el-button
        type="primary"
        @click="dialog.create = false"
        style="width: 100px; margin-right: 50px"
        >取消</el-button
      >
      <el-button
        type="primary"
        :loading="loading.create"
        @click="defaultMealUpdate"
        style="width: 100px"
        >保存</el-button
      >
    </div>

    <!-- 功率收费设置 -->
    <el-dialog
      title="设置收费标准"
      :visible.sync="dialog.powerSetVisible"
      @close="(dialog.powerSetVisible = false), (powerFormVisible = false)"
    >
      <el-form
        :model="powerForms"
        :rules="powerRules"
        ref="powerForm"
        label-position="top"
      >
        <el-form-item label="收费标准">
          <el-row>
            <el-col
              :span="6"
              v-for="(item, index) in mealDetail.chargePowerData"
              :key="index"
            >
              <div
                :class="powerSizeClass[index]"
                @click="changeChargeMealVisible(item, index)"
              >
                <div class="powerSizeTop">
                  <span>功率范围:</span>
                  {{ item.spower }}-{{ item.epower }}W
                </div>
                <div>
                  <div>
                    <span>峰值收费:</span>
                    {{ divide(item.chargePeakAmount,100) }}元
                  </div>
                  <div>
                    <span>低谷收费:</span>
                    {{ divide(item.chargeLowEbAmount,100) }}元
                  </div>
                </div>
              </div>
            </el-col>

            <el-col :span="24">
              <el-button
                type="primary"
                :loading="loading.create"
                @click="changeChargeMealVisible()"
                v-if="this.mealDetail.chargePowerData.length < 8"
                >新增</el-button
              >
              <el-button
                type="primary"
                :loading="loading.create"
                v-if="this.mealDetail.chargePowerData.length > 0"
                @click="delPowerSize"
                >删除</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>

        <el-row style="margin-top: 10px" v-if="powerFormVisible">
          <el-col :span="12">
            <el-row class="powerRates">
              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>功率段起始功率(W):</span>
              </el-col>
              <el-form-item prop="spower">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.spower"
                    maxlength="4"
                    type="number"
                    :disabled="powerForms.disabledSpower"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>功率段结束功率(W):</span>
              </el-col>
              <el-form-item prop="epower">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.epower"
                    maxlength="4"
                    type="number"
                    :disabled="powerForms.disabledEpower"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>峰值价格(元/每个收费时间间隔):</span>
              </el-col>
              <el-form-item prop="chargePeakAmount">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.chargePeakAmount"
                    maxlength="3"
                    type="number"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>低谷价格(元/每个收费时间间隔):</span>
              </el-col>

              <el-form-item prop="chargeLowEbAmount">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.chargeLowEbAmount"
                    maxlength="4"
                    type="number"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-button
                type="primary"
                :loading="loading.create"
                @click="powerFormVisible = false"
                style="margin-top: 10px"
                >取消</el-button
              >
              <el-button
                type="primary"
                :loading="loading.create"
                @click="changeChargeMeal()"
                style="margin-top: 10px"
                >确定输入</el-button
              >
            </el-row>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          :loading="loading.powerSetVisible"
          @click="(dialog.powerSetVisible = false), (powerFormVisible = false)"
          >确定</el-button
        >
      </div>
    </el-dialog>

    <!-- 服务费设置 -->
    <el-dialog
      title="设置服务费标准"
      :visible.sync="dialog.serveShow"
      @close="(dialog.serveShow = false), (powerFormVisible = false)"
    >
      <el-form
        :model="powerForms"
        :rules="powerRules"
        ref="powerForm"
        label-position="top"
      >
        <el-form-item label="收费标准">
          <el-row>
            <el-col
              :span="6"
              v-for="(item, index) in mealDetail.serviceData"
              :key="index"
            >
              <div
                :class="powerSizeClass[index]"
                @click="changeChargeMealVis2(item, index)"
              >
                <div class="powerSizeTop">
                  <span>功率范围:</span>
                  {{ item.spower }}-{{ item.epower }}W
                </div>
                <div>
                  <div>
                    <span>收费:</span>
                    {{ item.chargePeakAmount / 100 }}元
                  </div>
                </div>
              </div>
            </el-col>

            <el-col :span="24">
              <el-button
                type="primary"
                :loading="loading.create"
                @click="changeChargeMealVis2()"
                v-if="this.mealDetail.serviceData.length < 8"
                >新增</el-button
              >
              <el-button
                type="primary"
                :loading="loading.create"
                v-if="this.mealDetail.serviceData.length > 0"
                @click="delPowerSize2"
                >删除</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>

        <el-row style="margin-top: 10px" v-if="powerFormVisible">
          <el-col :span="12">
            <el-row class="powerRates">
              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>功率段起始功率(W):</span>
              </el-col>
              <el-form-item prop="spower">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.spower"
                    maxlength="4"
                    type="number"
                    :disabled="powerForms.disabledSpower"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>功率段结束功率(W):</span>
              </el-col>
              <el-form-item prop="epower">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.epower"
                    maxlength="4"
                    type="number"
                    :disabled="powerForms.disabledEpower"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-col :span="10" style="text-align: center; margin-top: 10px">
                <span>价格(元/每个收费时间间隔):</span>
              </el-col>
              <el-form-item prop="chargePeakAmount">
                <el-col :span="14" style="margin-top: 10px">
                  <el-input
                    clearable
                    v-model="powerForms.chargePeakAmount"
                    maxlength="3"
                    type="number"
                  ></el-input>
                </el-col>
              </el-form-item>

              <el-button
                type="primary"
                :loading="loading.create"
                @click="powerFormVisible = false"
                style="margin-top: 10px"
                >取消</el-button
              >
              <el-button
                type="primary"
                :loading="loading.create"
                @click="changeChargeMeal2()"
                style="margin-top: 10px"
                >确定输入</el-button
              >
            </el-row>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          :loading="loading.serveShow"
          @click="(dialog.serveShow = false), (powerFormVisible = false)"
          >确定</el-button
        >
      </div>
    </el-dialog>

    <!-- 展示二维码图片 -->
    <el-dialog title="二维码" :visible.sync="dialogVisible" width="500px">
      <img
        :src="showPic"
        alt
        style="width: 400px; display: block; margin: 0 auto 30px"
      />
    </el-dialog>
    <!-- dialog 查看图片弹窗 -->
    <el-dialog title :visible.sync="dialog.view">
      <el-carousel type="card" trigger="click" style="text-align: center">
        <el-carousel-item v-for="item in imgList" :key="item">
          <img :src="item" style="max-height: 310px; width: 350px" />
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
    <!-- 回到顶部 -->
    <BackToTop></BackToTop>
  </div>
</template>
<script>
import {
  getEquipmentList,
  addtem,
  delEquipment,
  updateItem,
  getEquipmentMeal,
  addEquipmentMeal,
  updateEquipmentExcel,
  addEquipmentCard,
  updateEquipmentCard,
  delEquipmentCard,
  getEquipmentCardList,
  updateTrumpet,
  downLoadExcel,
  getOneNetData,
  updateIsFlag,
  addOrUpdate,
} from "@/api/equipment";
import {
  updateSystemParamter,
  getSystemParamter,
  fileUpload,
  addSystemParamter,
} from "@/api/system";
import {
  getAddressByAreaId,
  getId,
  getAddress,
  getObjByName,
  getObjByAreaId,
  getByNameProvinceId,
  getByNameCityId,
  getByNameAreaId,
} from "@/utils/getAddress";
import { getBackAgentBox } from "@/api/adminEnt";
import { getEstateMonthlyRent } from "@/api/equipmentEle";
import BackToTop from "@/components/BackToTop";
import VePie from "v-charts/lib/pie";
import pcq from "@/components/index";
import Velocity from "velocity-animate";
import "velocity-animate/velocity.ui.js";
import { parseTime } from "@/utils";
import mixin from '@/utils/mathUtils.js'
export default {
  name: "equipment_defaultMeal",
  mixins: [mixin],
  components: {
    BackToTop,
    VePie,
    pcq,
  },
  data() {
    var validateNum = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("该处不能为空"));
      }
      setTimeout(() => {
        if (value <= 0) {
          callback(new Error("请输入大于0的正数"));
        } else {
          callback();
        }
      }, 1000);
    };
    var validateChargePeak = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var validateChargeLow = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var validateLowAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };

    var validateTallAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          if (Number(this.mealDetail.lowAmount * 100) >= Number(value * 100)) {
            callback(new Error("不能小于或等于 单次最低金额"));
          }
          callback();
        }
      }, 300);
    };
    var validateChargePowerData = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var valServicePeak = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var valServiceLow = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var valCustodyPeak = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("该处不能为空"));
      }
      setTimeout(() => {
        if (value <= 0) {
          callback(new Error("请输入大于0的正数"));
        } else {
          if (value < this.mealDetail.custodyLowEbAmount) {
            callback(
              new Error("请输入大于低谷时间段的收费金额(元/每个收费时间间隔)")
            );
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var valCustodyLow = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var valTallAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var valLowAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入正确金额,金额不能小于等0，两位小数以内!"));
        } else {
          if ((value + "").length > 12) {
            callback(new Error("金额超过范围,12位"));
          }
          callback();
        }
      }, 300);
    };
    var valSpower = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("起始功率不能为空!"));
      } else if (this.powerForms.epower <= this.powerForms.spower) {
        return callback(new Error("同一功率段起始功率应小于结束功率!"));
      }
      setTimeout(() => {
        var isNum = /^(0|([1-9]\d{0,2}))$/;

        if (!isNum.test(value)) {
          callback(new Error("请输入起始功率,起始功率应该为0-1000之间的整数"));
        } else {
          if ((value + "").length > 4) {
            callback(new Error("起始功率超过范围,4位"));
          }
          callback();
        }
      }, 300);
      // console.log("1");
    };
    var valEpower = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("结束功率不能为空!"));
      } else if (this.powerForms.epower <= this.powerForms.spower) {
        return callback(new Error("同一功率段结束功率应大于起始功率!"));
      }
      setTimeout(() => {
        var isNum = /^(0|([1-9]\d{0,2}))$/;
        if (!isNum.test(value)) {
          callback(new Error("请输入结束功率,结束功率应该为1-999之间的整数!"));
        } else {
          if ((value + "").length > 4) {
            callback(new Error("起始功率超过范围,4位"));
          }
          callback();
        }
      }, 300);
      // console.log("2");
    };
    var valChargeAmountTime = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("时间间隔不能为空!"));
      }
      setTimeout(() => {
        var isNum = /^(1|([1-9]\d*))$/;
        if (!isNum.test(value)) {
          callback(new Error("时间间隔为大于0的正整数!"));
        } else {
          if ((value + "").length > 5) {
            callback(new Error("时间间隔超过范围 5 位"));
          }
          callback();
        }
      }, 300);
    };
    var valChargeLowEbAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("低谷充电价格不能为空!"));
      } else if (
        this.powerForms.chargePeakAmount <= this.powerForms.chargeLowEbAmount
      ) {
        return callback(new Error("低谷充电价格应小于峰值充电价格!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,4}|[1-9][0-9]*\.\d{1,4})))$/;
        if (!isNum.test(value)) {
          callback(
            new Error(
              "请输入低谷充电价格,低谷充电价格不能小于等0，四位小数以内!"
            )
          );
        } else {
          if ((value + "").length > 6) {
            callback(new Error("低谷充电价格超过范围,6位"));
          }
          callback();
        }
      }, 300);
      // console.log("3");
    };
    var valChargePeakAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("峰值充电价格不能为空!"));
      } else if (
        this.powerForms.chargePeakAmount <= this.powerForms.chargeLowEbAmount
      ) {
        return callback(new Error("峰值充电价格应大于低谷充电价格!"));
      }
      setTimeout(() => {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,4}|[1-9][0-9]*\.\d{1,4})))$/;
        if (!isNum.test(value)) {
          callback(
            new Error(
              "请输入峰值充电价格,峰值充电价格不能小于等0，四位小数以内!"
            )
          );
        } else {
          if ((value + "").length > 6) {
            callback(new Error("峰值充电价格超过范围,6位"));
          }
          callback();
        }
      }, 300);
      // console.log("4");
    };
    return {
      //设备套餐表
      mealDetail: {
        mealId: null,
        equipmentId: null,
        chargeType: 1,
        chargeAmount: 0,
        chargePeakAmount: 0,
        chargeLowEbAmount: 0,
        serviceType: 0,
        serviceAmount: 0,
        servicePeakAmount: 0,
        serviceLowEbAmount: 0,
        custodyType: 0,
        custodyAmount: 0,
        custodyPeakAmount: 0,
        custodyLowEbAmount: 0,
        amountTime: 60,
        serviceTime: 60,
        lowAmount: 0,
        tallAmount: 0,
        peakStartTime: "00:00",
        peakEndTime: "00:00",
        lowEbStartTime: "00:00",
        lowEbEndTime: "00:00",
        // 功率表
        chargePowerData: [
          {
            chargeLowEbAmount: 100,
            chargePeakAmount: 100,
            spower: 0,
            epower: 150,
          },
          {
            chargeLowEbAmount: 120,
            chargePeakAmount: 120,
            spower: 150,
            epower: 200,
          },
          {
            chargePeakAmount: 140,
            chargeLowEbAmount: 140,
            spower: 200,
            epower: 250,
          },
          {
            chargeLowEbAmount: 160,
            chargePeakAmount: 160,
            spower: 250,
            epower: 300,
          },
          {
            chargeLowEbAmount: 180,
            chargePeakAmount: 180,
            spower: 300,
            epower: 350,
          },
          {
            chargeLowEbAmount: 200,
            chargePeakAmount: 200,
            spower: 350,
            epower: 400,
          },
          {
            chargeLowEbAmount: 220,
            chargePeakAmount: 220,
            spower: 400,
            epower: 450,
          },
        ],
        // 功率服务费表
        serviceData: [
        {
          chargePeakAmount: 100,
          spower: 0,
          epower: 150,
        },
        {
          chargePeakAmount: 200,
          spower: 150,
          epower: 200,
        },
        {
          chargePeakAmount: 300,
          spower: 200,
          epower: 250,
        },
        {
          chargePeakAmount: 400,
          spower: 250,
          epower: 300,
        },
        {
          chargePeakAmount: 500,
          spower: 300,
          epower: 350,
        },
        {
          chargePeakAmount: 600,
          spower: 350,
          epower: 400,
        },
        {
          chargePeakAmount: 700,
          spower: 400,
          epower: 450,
        },
      ],
      },
      defaultMealDetail: {
        mealId: null,
        equipmentId: null,
        chargeType: 2,
        chargeAmount: 0,
        chargePeakAmount: 0,
        chargeLowEbAmount: 0,
        serviceType: 0,
        serviceAmount: 0,
        servicePeakAmount: 0,
        serviceLowEbAmount: 0,
        custodyType: 0,
        custodyAmount: 0,
        custodyPeakAmount: 0,
        custodyLowEbAmount: 0,
        amountTime: 240,
        serviceTime: 240,
        lowAmount: 1,
        tallAmount: 5,
        peakStartTime: "00:00",
        peakEndTime: "00:00",
        lowEbStartTime: "00:00",
        lowEbEndTime: "00:00",
        // 功率表
        chargePowerData: [
          {
            chargeLowEbAmount: 100,
            chargePeakAmount: 100,
            spower: 0,
            epower: 150,
          },
          {
            chargeLowEbAmount: 120,
            chargePeakAmount: 120,
            spower: 150,
            epower: 200,
          },
          {
            chargePeakAmount: 140,
            chargeLowEbAmount: 140,
            spower: 200,
            epower: 250,
          },
          {
            chargeLowEbAmount: 160,
            chargePeakAmount: 160,
            spower: 250,
            epower: 300,
          },
          {
            chargeLowEbAmount: 180,
            chargePeakAmount: 180,
            spower: 300,
            epower: 350,
          },
          {
            chargeLowEbAmount: 200,
            chargePeakAmount: 200,
            spower: 350,
            epower: 400,
          },
          {
            chargeLowEbAmount: 220,
            chargePeakAmount: 220,
            spower: 400,
            epower: 450,
          },
        ],
        // 功率服务费表
        serviceData: [
        {
          chargePeakAmount: 100,
          spower: 0,
          epower: 150,
        },
        {
          chargePeakAmount: 200,
          spower: 150,
          epower: 200,
        },
        {
          chargePeakAmount: 300,
          spower: 200,
          epower: 250,
        },
        {
          chargePeakAmount: 400,
          spower: 250,
          epower: 300,
        },
        {
          chargePeakAmount: 500,
          spower: 300,
          epower: 350,
        },
        {
          chargePeakAmount: 600,
          spower: 350,
          epower: 400,
        },
        {
          chargePeakAmount: 700,
          spower: 400,
          epower: 450,
        },
      ],
      },
      defaultChargePowerData: [
        {
          chargeLowEbAmount: 100,
          chargePeakAmount: 100,
          spower: 0,
          epower: 150,
        },
        {
          chargeLowEbAmount: 120,
          chargePeakAmount: 120,
          spower: 150,
          epower: 200,
        },
        {
          chargePeakAmount: 140,
          chargeLowEbAmount: 140,
          spower: 200,
          epower: 250,
        },
        {
          chargeLowEbAmount: 160,
          chargePeakAmount: 160,
          spower: 250,
          epower: 300,
        },
        {
          chargeLowEbAmount: 180,
          chargePeakAmount: 180,
          spower: 300,
          epower: 350,
        },
        {
          chargeLowEbAmount: 200,
          chargePeakAmount: 200,
          spower: 350,
          epower: 400,
        },
        {
          chargeLowEbAmount: 220,
          chargePeakAmount: 220,
          spower: 400,
          epower: 450,
        },
      ],
      // 功率服务费表
      serviceData: [
        {
          chargePeakAmount: 100,
          spower: 0,
          epower: 150,
        },
        {
          chargePeakAmount: 200,
          spower: 150,
          epower: 200,
        },
        {
          chargePeakAmount: 300,
          spower: 200,
          epower: 250,
        },
        {
          chargePeakAmount: 400,
          spower: 250,
          epower: 300,
        },
        {
          chargePeakAmount: 500,
          spower: 300,
          epower: 350,
        },
        {
          chargePeakAmount: 600,
          spower: 350,
          epower: 400,
        },
        {
          chargePeakAmount: 700,
          spower: 400,
          epower: 450,
        },
      ],
      powerFormList: {},
      powerForms: {
        chargePeakAmount: "",
        chargeLowEbAmount: "",
        spower: "",
        epower: "",
        disabledSpower: false,
        disabledEpower: false,
      },
      powerFormVisible: false,
      powerSizeClass: [
        "powerSize",
        "powerSize",
        "powerSize",
        "powerSize",
        "powerSize",
        "powerSize",
        "powerSize",
        "powerSize",
      ],
      isCreate: "",
      EquipmentCardinfo: {
        status: 1,
        cardType: 2,
        area: "",
        city: "",
        equipmentId: "",
        timeDay: "",
        amount: "",
        address: "",
        cardTypeId: "",
        useTimeQuantum: "",
      },
      value: "",
      provices: [],
      cities: [],
      areas: [],
      pId: "",
      cId: "",
      dialogType: "",
      agentType: this.$store.state.user.userInfo.managerType,
      box: {
        area: "",
        province: "",
        city: "",
      },
      chartData: null,
      estateList: [],
      maintainList: [],
      loading: {
        list: false,
        add: false,
        detele: false,
        upd: false,
        create: false,
        addEquipmentCard: false,
        powerSetVisible: false,
        serveShow: false,
      },
      dialog: {
        bind: false,
        add: false,
        upd: false,
        view: false,
        create: false,
        addEquipmentCard: false,
        powerSetVisible: false,
        serveShow: false,
      },
      //套餐信息
      mealForm: {
        validateNum: "",
        valServicePeak: "",
        valServiceLow: "",
        valCustodyPeak: "",
        valCustodyLow: "",
        valLowAmount: "",
        valTallAmount: "",
      },
      // 套餐规则
      mealRules: {
        chargeAmount: [
          {
            required: true,
            message: "请输入充电收费价格(元/次)!",
            trigger: "change",
          },
          { validator: validateNum, trigger: "change" },
        ],
        serviceAmount: [
          {
            required: true,
            message: "请输入每次服务收费金额(元/次)!",
            trigger: "change",
          },
          { validator: validateNum, trigger: "change" },
        ],
        servicePeakAmount: [
          {
            required: true,
            message: "请输入峰值时间段服务收费金额(元/每个收费时间间隔)!",
            trigger: "change",
          },
          { validator: valServicePeak, trigger: "change" },
        ],
        serviceLowEbAmount: [
          {
            required: true,
            message: "请输入低谷时间段服务收费金额(元/每个收费时间间隔)!",
            trigger: "change",
          },
          { validator: valServiceLow, trigger: "change" },
        ],
        custodyAmount: [
          {
            required: true,
            message: "请输入每次保管收费金额(元/次)!",
            trigger: "change",
          },
          { validator: validateNum, trigger: "change" },
        ],
        custodyPeakAmount: [
          {
            required: true,
            message: "请输入峰值时间段保管收费金额(元/每个收费时间间隔)!",
            trigger: "change",
          },
          { validator: valCustodyPeak, trigger: "change" },
        ],
        custodyLowEbAmount: [
          {
            required: true,
            message: "请输入低谷时间段保管收费金额(元/每个收费时间间隔)!",
            trigger: "change",
          },
          { validator: valCustodyLow, trigger: "change" },
        ],
        amountTime: [
          {
            required: true,
            message: "请输入收费时间间隔",
            trigger: "blur",
          },
          { validator: valChargeAmountTime, trigger: "change" },
        ],
        serviceTime: [
          {
            required: true,
            message: "请输入收费时间间隔",
            trigger: "blur",
          },
          { validator: valChargeAmountTime, trigger: "change" },
        ],
        lowAmount: [
          {
            required: true,
            message: "请输入单次最低收费(元)!",
            trigger: "change",
          },
          { validator: validateLowAmount, trigger: "change" },
        ],
        tallAmount: [
          {
            required: true,
            message: "请输入单次最高收费(元)!",
            trigger: "change",
          },
          { validator: validateTallAmount, trigger: "change" },
        ],
        peakStartTime: [
          {
            required: true,
            message: "设置峰值起始时间(小时:分钟)",
            trigger: "change",
          },
        ],
        peakEndTime: [
          {
            required: true,
            message: "设置峰值结束时间(小时:分钟)",
            trigger: "change",
          },
        ],
      },
      // 功率段设置规则
      powerRules: {
        spower: [
          {
            required: true,
            message: "请输入起始功率(W)!",
            trigger: "change",
          },
          { validator: valSpower, trigger: "blur" },
        ],
        epower: [
          {
            required: true,
            message: "请输入结束功率(W)!",
            trigger: "change",
          },
          { validator: valEpower, trigger: "blur" },
        ],
        chargePeakAmount: [
          {
            required: true,
            message: "请输入峰值充电价格(元/每个收费时间间隔)!",
            trigger: "change",
          },
          { validator: valChargePeakAmount, trigger: "blur" },
        ],
        chargeLowEbAmount: [
          {
            required: true,
            message: "请输入低谷充电价格(元/每个收费时间间隔)!",
            trigger: "change",
          },
          { validator: valChargeLowEbAmount, trigger: "blur" },
        ],
      },

      imgList: [],
      total: 0,
      updataTip: "选择上传的设备列表",
      downloadLoading: false,
      updataLoading: false,
      exlDownloadLoading: false,
      date: "",
      listQuery: {
        offset: 1,
        limit: 10,
        equipmentNumber: "",
        deviceId: "",
        equipmentId: "",
        address: "",
        equipmentType: "",
        maxCellVoltage: "",
        equipmentStatus: "",
        startTime: "",
        endTime: "",
      },
      addQuery: {
        equipmentId: "",
        equipmentNumber: "",
        deviceId: "",
        estateId: 0,
        maintainId: 0,
        province: "",
        city: "",
        area: "",
        equipmentAddress: "",
        monthlyRent: 0,
      },
      bindQuery: {
        equipmentNumber: "",
        equipmentCode: "",
        equipmentType: "",
      },
      list: null,
      chargeRules: [],
      downloadLoading: false,
      equipmentCount: 0,
      showPic: "",
      dialogVisible: false,
      // 批量操作
      selectionView: false,
      equipmentIdsType: "",
      selectionArray: [],
      equipmentIds: null,
    };
  },

  methods: {
    getLocalTime(now) {
      let date = new Date(now);
      let D = date.getDate() + " ";
      let h = date.getHours() < 9 ? "0" + date.getHours() : date.getHours();
      let m =
        date.getMinutes() < 9 ? "0" + date.getMinutes() : date.getMinutes();
      let s = date.getSeconds();
      return h + ":" + m;
    },
    //查询有无默认套餐
    // 获取系统参数
    async getDefaultMeal() {
      try {
        this.systemId = "";
        const { data } = await getSystemParamter(8);
        // console.log(data)

        if (data.systemId != null) {
          this.systemId = data.systemId;
        }
        if (data.parameter == null) {
          this.isCreate = 2;
          this.$message.warning("您还未设置过默认套餐,请设置默认套餐!");
          this.mealDetail = this.defaultMealDetail;
        } else {
          this.isCreate = 1;
          this.$message.success("已有默认套餐，可更新!");
          this.mealDetail = JSON.parse(data.parameter);
          this.mealDetail.chargePowerData =
            this.mealDetail.chargePowerData == null
              ? this.defaultChargePowerData
              : JSON.parse(this.mealDetail.chargePowerData);
          this.mealDetail.serviceData =
            this.mealDetail.serviceData == null
              ? this.serviceData 
              : JSON.parse(this.mealDetail.serviceData);
          // this.mealDetail.serviceData = this.serviceData
          this.mealDetail.peakStartTime =
            this.mealDetail.peakStartTime == null
              ? "00:00"
              : this.getLocalTime(this.mealDetail.peakStartTime);
          this.mealDetail.peakEndTime =
            this.mealDetail.peakEndTime == null
              ? "00:00"
              : this.getLocalTime(this.mealDetail.peakEndTime);
          this.mealDetail.lowEbStartTime =
            this.mealDetail.lowEbStartTime == null
              ? "00:00"
              : this.getLocalTime(this.mealDetail.lowEbStartTime);
          this.mealDetail.lowEbEndTime == null
            ? "00:00"
            : this.getLocalTime(this.mealDetail.lowEbEndTime);
          this.mealDetail.chargeAmount =
            this.mealDetail.chargeAmount == null
              ? 0
              : this.mealDetail.chargeAmount / 100;
          (this.mealDetail.serviceAmount =
            this.mealDetail.serviceAmount == null
              ? 0
              : this.mealDetail.serviceAmount / 100),
            (this.mealDetail.servicePeakAmount =
              this.mealDetail.servicePeakAmount == null
                ? 0
                : this.mealDetail.servicePeakAmount / 100);
          this.mealDetail.serviceLowEbAmount =
            this.mealDetail.serviceLowEbAmount == null
              ? 0
              : this.mealDetail.serviceLowEbAmount / 100;
          this.mealDetail.custodyAmount =
            this.mealDetail.custodyAmount == null
              ? 0
              : this.mealDetail.custodyAmount / 100;
          this.mealDetail.custodyPeakAmount =
            this.mealDetail.custodyPeakAmount == null
              ? 0
              : this.mealDetail.custodyPeakAmount / 100;
          this.mealDetail.custodyLowEbAmount =
            this.mealDetail.custodyLowEbAmount == null
              ? 0
              : this.mealDetail.custodyLowEbAmount / 100;
          this.mealDetail.amountTime =
            this.mealDetail.amountTime == null
              ? 240
              : this.mealDetail.amountTime;
          this.mealDetail.serviceTime =
            this.mealDetail.serviceTime == null
              ? 240
              : this.mealDetail.serviceTime;
          this.mealDetail.lowAmount =
            this.mealDetail.lowAmount == null
              ? ""
              : this.mealDetail.lowAmount / 100;
          this.mealDetail.tallAmount =
            this.mealDetail.tallAmount == null
              ? ""
              : this.mealDetail.tallAmount / 100;
        }
      } catch (err) {
        // console.log(err);
      }
    },

    //默认套餐修改
    async defaultMealUpdate() {
      try {
        this.$refs["mealForm"].validate(async (valid) => {
          this.loading.Create = true;
          let query = {
            id: this.systemId,
            mealId: this.mealDetail.mealId,
            equipmentId: this.mealDetail.equipmentId,
            chargeType: this.mealDetail.chargeType
              ? this.mealDetail.chargeType
              : 1,
            chargeAmount: parseFloat(
              (this.mealDetail.chargeAmount * 100).toFixed(2)
            ),
            chargePowerData:
              this.mealDetail.chargePowerData == []
                ? this.defaultChargePowerData
                : JSON.stringify(this.mealDetail.chargePowerData),
            // 新增两个参数 2024.12.31
            serviceData:
              this.mealDetail.serviceData == []
                ? this.serviceData
                : JSON.stringify(this.mealDetail.serviceData),
            serviceTime: this.mealDetail.serviceTime
              ? this.mealDetail.serviceTime
              : 60,
            serviceType: this.mealDetail.serviceType
              ? this.mealDetail.serviceType
              : 0,
            serviceAmount: parseFloat(
              (this.mealDetail.serviceAmount * 100).toFixed(2)
            ),
            servicePeakAmount: parseFloat(
              (this.mealDetail.servicePeakAmount * 100).toFixed(2)
            ),
            serviceLowEbAmount: parseFloat(
              (this.mealDetail.serviceLowEbAmount * 100).toFixed(2)
            ),

            custodyType: this.mealDetail.custodyType
              ? this.mealDetail.custodyType
              : 0,
            custodyAmount: parseFloat(
              (this.mealDetail.custodyAmount * 100).toFixed(2)
            ),
            custodyPeakAmount: parseFloat(
              (this.mealDetail.custodyPeakAmount * 100).toFixed(2)
            ),
            custodyLowEbAmount: parseFloat(
              (this.mealDetail.custodyLowEbAmount * 100).toFixed(2)
            ),

            peakStartTime: this.mealDetail.peakStartTime
              ? this.mealDetail.peakStartTime
              : null,
            peakEndTime: this.mealDetail.peakEndTime
              ? this.mealDetail.peakEndTime
              : null,
            lowEbStartTime: this.mealDetail.peakEndTime
              ? this.mealDetail.peakEndTime
              : null,
            lowEbEndTime: this.mealDetail.peakStartTime
              ? this.mealDetail.peakStartTime
              : null,

            amountTime: this.mealDetail.amountTime
              ? this.mealDetail.amountTime
              : 60,

            lowAmount: parseFloat((this.mealDetail.lowAmount * 100).toFixed(2)),
            tallAmount: parseFloat(
              (this.mealDetail.tallAmount * 100).toFixed(2)
            ),
          };

          const data = await addOrUpdate(query);
          // console.log(data)
          // console.log(data.code,this.isCreate)

          if (data.code == 1 && this.isCreate == 1) {
            this.$message({
              type: "success",
              message: "更新成功",
            });
          } else if (data.code == 1 && this.isCreate == 2) {
            this.$message({
              type: "success",
              message: "添加成功",
            });
          }
        });
      } catch (err) {}
    },
    // 添加/修改套餐
    async crateMeal() {
      this.$refs["mealForm"].validate(async (valid) => {
        // console.log(valid);
        // console.log(this.isCreate);
        if (valid) {
          try {
            if (this.isCreate == 1) {
              //调用服务器接口
              const data = await updateEquipmentExcel(query);

              if (data.code == 1) {
                this.$message({
                  type: "success",
                  message: "更新成功",
                });
              }
            } else if (this.isCreate == 2) {
              //调用服务器接口
              const data = await addEquipmentMeal(query);
              // console.log("添加成功");
              if (data.code == 1) {
                this.$message({
                  type: "success",
                  message: "添加成功",
                });
              }
            }
            this.dialog.create = false;
            this.loading.create = false;
          } catch (error) {
            this.loading.create = false;
            this.dialog.create = false;
            this.resetCreateForm();
          }
        }
      });
    },
    resetCreateForm(flag) {
      this.$refs.mealForm && this.$refs.mealForm.resetFields();
      // console.log(this.mealDetail);
      if (flag) {
        this.mealDetail = {
          equipmentId: "",
        };
      }
    },

    //批量选择显示
    selectionShow(e) {
      this.selectionView = true;
      this.toggleSelection();
      if (e === 1) {
        // 设备套餐
        this.equipmentIdsType = 1;
      } else if (e == 2) {
        // 设备卡套餐
        this.equipmentIdsType = 2;
      }
    },
    //批量选择
    handleSelectionChange(e) {
      // console.log(e);
      this.selectionArray = e;
    },
    // table取消选择
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    // 拼接设备id
    getEquipmentIds() {
      this.mealDetail = {
        mealId: null,
        equipmentId: null,
        chargeType: 1,
        chargeAmount: 0,
        chargePeakAmount: 0,
        chargeLowEbAmount: 0,
        serviceType: 0,
        serviceAmount: 0,
        servicePeakAmount: 0,
        serviceLowEbAmount: 0,
        custodyType: 0,
        custodyAmount: 0,
        custodyPeakAmount: 0,
        custodyLowEbAmount: 0,
        amountTime: 60,
        serviceTime: 60,
        lowAmount: 0,
        tallAmount: 0,
        peakStartTime: "00:00",
        peakEndTime: "00:00",
        lowEbStartTime: "00:00",
        lowEbEndTime: "00:00",
        chargePowerData: [
          {
            chargeLowEbAmount: 100,
            chargePeakAmount: 100,
            spower: 0,
            epower: 150,
          },
          {
            chargeLowEbAmount: 120,
            chargePeakAmount: 120,
            spower: 150,
            epower: 200,
          },
          {
            chargePeakAmount: 140,
            chargeLowEbAmount: 140,
            spower: 200,
            epower: 250,
          },
          {
            chargeLowEbAmount: 160,
            chargePeakAmount: 160,
            spower: 250,
            epower: 300,
          },
          {
            chargeLowEbAmount: 180,
            chargePeakAmount: 180,
            spower: 300,
            epower: 350,
          },
          {
            chargeLowEbAmount: 200,
            chargePeakAmount: 200,
            spower: 350,
            epower: 400,
          },
          {
            chargeLowEbAmount: 220,
            chargePeakAmount: 220,
            spower: 400,
            epower: 450,
          },
        ],
        // 功率服务费表
        serviceData: [
        {
          chargePeakAmount: 100,
          spower: 0,
          epower: 150,
        },
        {
          chargePeakAmount: 200,
          spower: 150,
          epower: 200,
        },
        {
          chargePeakAmount: 300,
          spower: 200,
          epower: 250,
        },
        {
          chargePeakAmount: 400,
          spower: 250,
          epower: 300,
        },
        {
          chargePeakAmount: 500,
          spower: 300,
          epower: 350,
        },
        {
          chargePeakAmount: 600,
          spower: 350,
          epower: 400,
        },
        {
          chargePeakAmount: 700,
          spower: 400,
          epower: 450,
        },
      ],
      };
      if (this.selectionArray.length == 0 || this.selectionArray == null) {
        this.$message({
          type: "error",
          message: "请选择需要设置套餐的设备",
        });
      } else if (this.selectionArray.length > 0) {
        for (var i = 0; i < this.selectionArray.length; i++) {
          this.equipmentIds =
            this.equipmentIds == null
              ? (this.equipmentIds = this.selectionArray[0].equipmentId)
              : this.equipmentIds + "," + this.selectionArray[i].equipmentId;
        }

        if (this.equipmentIdsType == 1) {
          this.dialog.create = true;
        } else if (this.equipmentIdsType == 2) {
          this.dialog.addEquipmentCard = true;
        }
      }
    },

    //
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          return v[j];
        })
      );
    },

    selectAgentBox() {
      var pr = "";
      var ci = "";
      var ar = "";
      var type = 0;

      if (this.box.province) {
        pr = this.box.province;
      }
      if (this.box.city) {
        ci = this.box.city;
      }
      if (this.box.area) {
        ar = this.box.area;
      }
      //wuye

      type = 3;
      this.getAgentBox(type, pr, ci, ar);
    },

    async getAgentBox(agentType, province, city, area) {
      //调用服务器接口
      const { data } = await getBackAgentBox({
        agentType: agentType,
        province: province,
        city: city,
        area: area,
      });

      this.estateList = data;
      var flag = false;
      if (data != "") {
        for (var item in data) {
          if (this.addQuery.estateId == data[item].agentId) {
            flag = true;
            return;
          }
        }
      }
      if (!flag) {
        this.addQuery.estateId = "";
      }
    },

    //添加设备
    async add() {
      this.$refs["bindForm"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading.add = true;
            let query = {
              deviceId: this.addQuery.deviceId,
              equipmentNumber: this.addQuery.equipmentNumber,
              equipmentId: this.addQuery.equipmentId,
              equipmentType: this.addQuery.equipmentType,
              province: getAddress(this.addQuery.province),
              city: getAddress(this.addQuery.city),
              area: getAddress(this.addQuery.area),
              monthlyRent: this.addQuery.monthlyRent
                ? this.addQuery.monthlyRent * 100
                : 0,
              equipmentAddress: this.addQuery.equipmentAddress,
              estateId: this.addQuery.estateId ? this.addQuery.estateId : "",
              maintainId: this.addQuery.maintainId
                ? this.addQuery.maintainId
                : "",
            };
            this.loading.add = true;
            if (this.dialogType === 1) {
              //调用服务器接口
              const { data } = await addtem(query);
            } else {
              //调用服务器接口
              const { data } = await updateItem(query);
            }
            this.$message({
              type: "success",
              message: "添加成功",
            });
            this.dialog.add = false;
            this.loading.add = false;
            this.resetForm();
          } catch (error) {
            this.loading.add = false;
            //  this.resetForm();
          }
        } else {
          return false;
        }
      });
    },

    changeChargeMealVisible(item, index) {
      this.powerFormVisible = true;
      if (item) {
        /**
         * 2022.08.15  唐
         * log:变更无法修改套餐高峰低谷间隔问题
         * */
        // this.powerForms = {};
        
        for (let j = 0; j < this.mealDetail.chargePowerData.length; j++) {
          this.powerSizeClass[j] = "powerSize";
          if (j == index) {
            this.powerSizeClass[j] = "powerSizeSel";
          }
        }
        this.powerFormList.chargePeakAmount = this.divide(item.chargePeakAmount,100);
        this.powerFormList.chargeLowEbAmount = this.divide(item.chargeLowEbAmount,100);
        this.powerFormList.spower = item.spower;
        this.powerFormList.epower = item.epower;
        this.powerFormList.index = index;

        // this.powerForms = this.powerFormList;

        this.powerForms.chargePeakAmount = this.divide(item.chargePeakAmount,100);
        this.powerForms.chargeLowEbAmount = this.divide(item.chargeLowEbAmount,100);
        this.powerForms.spower = item.spower;
        this.powerForms.epower = item.epower;
        this.powerForms.index = index;
        // console.log(this.mealDetail.chargePowerData.length - 1, index);
        if (index == this.mealDetail.chargePowerData.length - 1) {
          this.powerForms.disabledSpower = true;
          this.powerForms.disabledEpower = false;
        } else if (index < this.mealDetail.chargePowerData.length) {
          this.powerForms.disabledSpower = true;
          this.powerForms.disabledEpower = true;
        }
      } else {
        // 新增
        // console.log("changeChargeMealVisible2");
        this.powerFormList = {};
        this.powerForms = {};
        // 衔接上一个功率段
        this.powerForms.spower =
          this.mealDetail.chargePowerData[
            this.mealDetail.chargePowerData.length - 1
          ].epower;
        this.powerForms.disabledSpower = true;
      }
    },
    changeChargeMealVis2(item, index) {
      this.powerFormVisible = true;
      if (item) {
        for (let j = 0; j < this.mealDetail.serviceData.length; j++) {
          this.powerSizeClass[j] = "powerSize";
          if (j == index) {
            this.powerSizeClass[j] = "powerSizeSel";
          }
        }
        this.powerFormList.chargePeakAmount = item.chargePeakAmount / 100;
        this.powerFormList.spower = item.spower;
        this.powerFormList.epower = item.epower;
        this.powerFormList.index = index;

        // this.powerForms = this.powerFormList;

        this.powerForms.chargePeakAmount = item.chargePeakAmount / 100;
        this.powerForms.spower = item.spower;
        this.powerForms.epower = item.epower;
        this.powerForms.index = index;
        // console.log(this.mealDetail.serviceData.length - 1, index);
        if (index == this.mealDetail.serviceData.length - 1) {
          this.powerForms.disabledSpower = true;
          this.powerForms.disabledEpower = false;
        } else if (index < this.mealDetail.serviceData.length) {
          this.powerForms.disabledSpower = true;
          this.powerForms.disabledEpower = true;
        }
      } else {
        // 新增
        // console.log("changeChargeMealVisible2");
        this.powerFormList = {};
        this.powerForms = {};
        // 衔接上一个功率段
        this.powerForms.spower =
          this.mealDetail.serviceData[
            this.mealDetail.serviceData.length - 1
          ].epower;
        this.powerForms.disabledSpower = true;
      }
    },
    changeChargeMeal() {
      // console.log("valid");
      // this.$refs["powerForm"].validate(async(valid) => {
      // console.log(valid)
      // if (valid) {
      // 修改
      if (this.powerFormList.chargePeakAmount) {
        // console.log("changeChargeMealVisible3");
        // console.log(this.powerFormList);
        // console.log(this.powerFormList.chargePeakAmount);
        let index = this.powerFormList.index;
        let item = this.powerFormList.item;
        for (let i = 0; i < this.mealDetail.chargePowerData.length; i++) {
          if (i == index) {
            this.mealDetail.chargePowerData[i].chargePeakAmount =
            this.multiply(this.powerForms.chargePeakAmount,100)
              // this.powerForms.chargePeakAmount * 100;
            this.mealDetail.chargePowerData[i].chargeLowEbAmount =
            this.multiply(this.powerForms.chargeLowEbAmount,100)
              // this.powerForms.chargeLowEbAmount * 100;
            this.mealDetail.chargePowerData[i].spower = this.powerForms.spower;
            this.mealDetail.chargePowerData[i].epower = this.powerForms.epower;
          }
        }
      } else {
        // 新增
        this.mealDetail.chargePowerData.push({
          chargePeakAmount: this.multiply(this.powerForms.chargePeakAmount,100),
          chargeLowEbAmount: this.multiply(this.powerForms.chargeLowEbAmount,100),
          spower: this.powerForms.spower,
          epower: this.powerForms.epower,
        });
      }
      // 发请求改设备设置
      // }
      // });

      this.powerFormVisible = false;
    },
    // 删除功率段
    delPowerSize() {
      this.powerFormVisible = false;
      this.powerForms = {
        chargePeakAmount: "",
        chargeLowEbAmount: "",
        spower: "",
        epower: "",
        disabledSpower: false,
        disabledEpower: false,
      };

      // console.log(
      //   this.mealDetail.chargePowerData.length - 1,
      //   this.mealDetail.chargePowerData.length - 2
      // );
      if (this.mealDetail.chargePowerData.length > 2) {
        this.mealDetail.chargePowerData.length =
          this.mealDetail.chargePowerData.splice(
            this.mealDetail.chargePowerData.length - 1,
            this.mealDetail.chargePowerData.length - 2
          );
      } else if (this.mealDetail.chargePowerData.length == 2) {
        this.mealDetail.chargePowerData =
          this.mealDetail.chargePowerData.splice(0, 1);
      } else if (this.mealDetail.chargePowerData.length == 1) {
        this.mealDetail.chargePowerData = [];
      }
    },
    // 判断数字有没有超过两位小数
    hasMoreThanTwoDecimals(num) {
      let strNum = num.toString();
      let decimalIndex = strNum.indexOf('.');
      if (decimalIndex === -1) {
        return false;
      }
      let decimalPart = strNum.slice(decimalIndex + 1);
      return decimalPart.length > 2;
    },
    changeChargeMeal2() {
      // console.log("valid");
      // this.$refs["powerForm"].validate(async(valid) => {
      // console.log(valid)
      if(this.hasMoreThanTwoDecimals(this.powerForms.chargePeakAmount)) return this.$message.warning('价格只能保留两位小数！')
      // if (valid) {
      // 修改
      if (this.powerFormList.chargePeakAmount) {
        // console.log("changeChargeMealVisible3");
        // console.log(this.powerFormList);
        // console.log(this.powerFormList.chargePeakAmount);
        let index = this.powerFormList.index;
        let item = this.powerFormList.item;
        for (let i = 0; i < this.mealDetail.serviceData.length; i++) {
          if (i == index) {
            this.mealDetail.serviceData[i].chargePeakAmount =
              this.powerForms.chargePeakAmount * 100;
            this.mealDetail.serviceData[i].spower = this.powerForms.spower;
            this.mealDetail.serviceData[i].epower = this.powerForms.epower;
          }
        }
      } else {
        // 新增
        this.mealDetail.serviceData.push({
          chargePeakAmount: this.powerForms.chargePeakAmount * 100,
          spower: this.powerForms.spower,
          epower: this.powerForms.epower,
        });
      }
      // 发请求改设备设置
      // }
      // });

      this.powerFormVisible = false;
    },
    // 删除功率段
    delPowerSize2() {
      this.powerFormVisible = false;
      this.powerForms = {
        chargePeakAmount: "",
        chargeLowEbAmount: "",
        spower: "",
        epower: "",
        disabledSpower: false,
        disabledEpower: false,
      };

      // console.log(
      //   this.mealDetail.serviceData.length - 1,
      //   this.mealDetail.serviceData.length - 2
      // );
      if (this.mealDetail.serviceData.length > 2) {
        this.mealDetail.serviceData.length =
          this.mealDetail.serviceData.splice(
            this.mealDetail.serviceData.length - 1,
            this.mealDetail.serviceData.length - 2
          );
      } else if (this.mealDetail.serviceData.length == 2) {
        this.mealDetail.serviceData =
          this.mealDetail.serviceData.splice(0, 1);
      } else if (this.mealDetail.serviceData.length == 1) {
        this.mealDetail.serviceData = [];
      }
    },
    resetPowerForm(flag) {
      this.$refs.powerForm && this.$refs.powerForm.resetFields();
      if (flag) {
        this.powerForms = {
          chargePeakAmount: "",
          chargeLowEbAmount: "",
          spower: "",
          epower: "",
        };
      }
    },

    // 批量下载设备二维码
    downloadZipQr() {
      window.location.href =
        this.$uploadUrl + "api/equipment/admin/downloadZipQr";
    },
    resetForm(flag) {
      this.$refs.bindForm && this.$refs.bindForm.resetFields();
      this.addQuery = {
        equipmentNumber: "",
        companyId: "",
        city: "",
        area: "",
        equipmentAddress: "",
        estateId: "",
      };
    },
    //禁用启用
    updateIsFlag(equipmentId, deviceId, status) {
      let type;
      if (status) {
        type = status == 1 ? "启用" : "禁用";
      } else {
        type = "启用";
      }
      this.$confirm(`确定${type}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //调用服务器接口
          const { data } = await updateIsFlag({
            equipmentId: equipmentId,
            deviceId: deviceId,
            isFlag: status,
          });
          this.$message({
            type: "success",
            message: `${type}成功!`,
          });
        })
        .catch(() => {});
    },
    beforOpenDialog(type, item) {
      if (type === 1) {
        this.resetForm();
      } else {
        this.resetForm();
        const {
          equipmentId,
          equipmentNumber,
          deviceId,
          equipmentType,
          province,
          city,
          area,
          equipmentAddress,
          estateId,
          maintainId,
          monthlyRent,
        } = item;
        this.addQuery = {
          ...this.addQuery,
          equipmentId,
          equipmentNumber,
          deviceId,
          equipmentType,
          province,
          city,
          area,
          equipmentAddress,
          estateId,
          maintainId,
          monthlyRent,
          deviceId,
        };
        this.box.province = this.addQuery.province;
        this.box.city = this.addQuery.city;
        this.box.area = this.addQuery.area;
        this.addQuery.monthlyRent = this.addQuery.monthlyRent / 100 + "";
        this.addQuery.province = getByNameProvinceId(province);
        this.addQuery.city = getByNameCityId(province, city);
        this.addQuery.area = getByNameAreaId(province, city, area);
        this.getCities2();
        this.getAreas2();
      }

      this.selectAgentBox();
      this.dialogType = type;
      this.addQuery.province = getObjByName(
        this.$store.state.user.userInfo.province
      )[0].AREA_ID;

      this.getCities2();
      this.getAreas2();
      this.dialog.add = true;
    },
  },
  computed: {
    diglogTitle() {
      return this.dialogType === 1 ? "新增设备" : "编辑设备";
    },
  },
  created() {
    this.getDefaultMeal();
  },
};
</script>
<style lang="scss" scoped>
.el-button + .el-button {
  margin-left: 0px;
}
.powerSize {
  line-height: 2;
  margin-right: 10px;
  margin-bottom: 10px;
  border-radius: 10px;
  border: 1px solid #dcdfe6;
  text-align: center;
}
.powerSizeSel {
  line-height: 2;
  margin-right: 10px;
  margin-bottom: 10px;
  border-radius: 10px;
  border: 1px solid #4e9aff;
  text-align: center;
}
.powerSizeTop {
  border-bottom: 1px solid #dcdfe6;
}
.powerRates {
  padding: 10px;
  line-height: 2;
  text-align: center;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
}
.powerSizeInp {
  width: 50px;
  height: 50px;
  margin: 5px;
}
.powerSizeTitle {
  display: flex;
  flex-wrap: wrap;
}
</style>
